package com.example.it.studyviewflipper;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import java.util.List;


public class ImageGridViewAdapter extends BaseAdapter {

    private Context context;
    private  List<Integer> imageIdList;

    //初始化构造
    public ImageGridViewAdapter(Context context, List<Integer> imageIdList) {
        this.context = context;
        this.imageIdList = imageIdList;
    }

    //获取全部视图的数量
    @Override
    public int getCount() {
        return imageIdList.size();
    }

    @Override
    public Object getItem(int i) {
        return this.imageIdList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    /**
     * 获取到View并返回
     */
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View currentVieww = view;
        if (currentVieww == null) {
            currentVieww = View.inflate(this.context, R.layout.item_image_gridview, null);
        }
        ImageView ivPet = (ImageView) currentVieww.findViewById(R.id.ivPet);
        int imageId = this.imageIdList.get(i);
        ivPet.setImageResource(imageId);
        //将动画的id放到我们图片的tag属性中方便我们在点击事件中拿到
        currentVieww.setTag(i);

        return currentVieww;
    }

}
